import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.*;


public class AdmissionSystem {

    private Map<Integer, MajorGroup> majorGroups = new HashMap<>();  // 存储所有可选的学校和专业
    private List<VoluntaryChoice> voluntaryChoices = new ArrayList<>();  // 存储学生已选择的志愿

    // 初始化高校和专业
    private void initializeMajorGroups() {
        // 志愿组1 - 北京大学
        MajorGroup pekingUniversity = new MajorGroup("北京大学");
        pekingUniversity.addMajor(new Major("数学与应用数学"));
        pekingUniversity.addMajor(new Major("法学"));
        pekingUniversity.addMajor(new Major("临床医学"));
        pekingUniversity.addMajor(new Major("汉语言文学"));
        pekingUniversity.addMajor(new Major("经济学"));
        majorGroups.put(1, pekingUniversity);

        // 志愿组2 - 清华大学
        MajorGroup tsinghuaUniversity = new MajorGroup("清华大学");
        tsinghuaUniversity.addMajor(new Major("机械工程"));
        tsinghuaUniversity.addMajor(new Major("计算机科学与技术"));
        tsinghuaUniversity.addMajor(new Major("法学"));
        tsinghuaUniversity.addMajor(new Major("土木工程"));
        tsinghuaUniversity.addMajor(new Major("电气工程"));
        majorGroups.put(2, tsinghuaUniversity);

        // 志愿组3 - 中国人民大学
        MajorGroup rucUniversity = new MajorGroup("中国人民大学");
        rucUniversity.addMajor(new Major("经济学"));
        rucUniversity.addMajor(new Major("法学"));
        rucUniversity.addMajor(new Major("新闻学"));
        rucUniversity.addMajor(new Major("统计学"));
        majorGroups.put(3, rucUniversity);

        // 志愿组4 - 北京师范大学
        MajorGroup bnuUniversity = new MajorGroup("北京师范大学");
        bnuUniversity.addMajor(new Major("教育学"));
        bnuUniversity.addMajor(new Major("数学"));
        bnuUniversity.addMajor(new Major("汉语言文学"));
        bnuUniversity.addMajor(new Major("心理学"));
        majorGroups.put(4, bnuUniversity);

        // 志愿组5 - 北京航空航天大学
        MajorGroup beihangUniversity = new MajorGroup("北京航空航天大学");
        beihangUniversity.addMajor(new Major("飞行器设计"));
        beihangUniversity.addMajor(new Major("电子信息工程"));
        beihangUniversity.addMajor(new Major("计算机"));
        beihangUniversity.addMajor(new Major("自动化"));
        majorGroups.put(5, beihangUniversity);

        // 志愿组6 - 北京理工大学
        MajorGroup bitUniversity = new MajorGroup("北京理工大学");
        bitUniversity.addMajor(new Major("武器系统"));
        bitUniversity.addMajor(new Major("电子科学"));
        bitUniversity.addMajor(new Major("计算机"));
        bitUniversity.addMajor(new Major("自动化"));
        majorGroups.put(6, bitUniversity);

        // 志愿组7 - 中国农业大学
        MajorGroup cauUniversity = new MajorGroup("中国农业大学");
        cauUniversity.addMajor(new Major("农学"));
        cauUniversity.addMajor(new Major("动物科学"));
        cauUniversity.addMajor(new Major("农业机械化"));
        cauUniversity.addMajor(new Major("食品科学"));
        majorGroups.put(7, cauUniversity);

        // 志愿组8 - 北京科技大学
        MajorGroup bjutUniversity = new MajorGroup("北京科技大学");
        bjutUniversity.addMajor(new Major("材料科学"));
        bjutUniversity.addMajor(new Major("机械"));
        bjutUniversity.addMajor(new Major("计算机"));
        bjutUniversity.addMajor(new Major("能源动力"));
        majorGroups.put(8, bjutUniversity);

        // 志愿组9 - 北京交通大学
        MajorGroup bjtUniversity = new MajorGroup("北京交通大学");
        bjtUniversity.addMajor(new Major("交通运输"));
        bjtUniversity.addMajor(new Major("通信工程"));
        bjtUniversity.addMajor(new Major("计算机"));
        bjtUniversity.addMajor(new Major("土木工程"));
        majorGroups.put(9, bjtUniversity);

        // 志愿组10 - 北京邮电大学
        MajorGroup buptUniversity = new MajorGroup("北京邮电大学");
        buptUniversity.addMajor(new Major("电子信息"));
        buptUniversity.addMajor(new Major("计算机"));
        buptUniversity.addMajor(new Major("自动化"));
        buptUniversity.addMajor(new Major("通信"));
        majorGroups.put(10, buptUniversity);

        // 志愿组11 - 中央财经大学
        MajorGroup cufeUniversity = new MajorGroup("中央财经大学");
        cufeUniversity.addMajor(new Major("经济学"));
        cufeUniversity.addMajor(new Major("金融学"));
        cufeUniversity.addMajor(new Major("财政学"));
        cufeUniversity.addMajor(new Major("会计学"));
        majorGroups.put(11, cufeUniversity);

        // 志愿组12 - 对外经济贸易大学
        MajorGroup uibeUniversity = new MajorGroup("对外经济贸易大学");
        uibeUniversity.addMajor(new Major("国际经济"));
        uibeUniversity.addMajor(new Major("金融学"));
        uibeUniversity.addMajor(new Major("英语"));
        uibeUniversity.addMajor(new Major("法学"));
        majorGroups.put(12, uibeUniversity);

        // 志愿组13 - 北京化工大学
        MajorGroup buctUniversity = new MajorGroup("北京化工大学");
        buctUniversity.addMajor(new Major("化学工程"));
        buctUniversity.addMajor(new Major("高分子材料"));
        buctUniversity.addMajor(new Major("生物工程"));
        buctUniversity.addMajor(new Major("环境工程"));
        majorGroups.put(13, buctUniversity);

        // 志愿组14 - 北京工业大学
        MajorGroup bjutUniversity2 = new MajorGroup("北京工业大学");
        bjutUniversity2.addMajor(new Major("机械"));
        bjutUniversity2.addMajor(new Major("电子信息"));
        bjutUniversity2.addMajor(new Major("计算机"));
        bjutUniversity2.addMajor(new Major("土木"));
        majorGroups.put(14, bjutUniversity2);

        // 志愿组15 - 北京外国语大学
        MajorGroup bfuuUniversity = new MajorGroup("北京外国语大学");
        bfuuUniversity.addMajor(new Major("英语"));
        bfuuUniversity.addMajor(new Major("法语"));
        bfuuUniversity.addMajor(new Major("德语"));
        bfuuUniversity.addMajor(new Major("翻译"));
        majorGroups.put(15, bfuuUniversity);

        // 志愿组16 - 中国传媒大学
        MajorGroup cumtUniversity = new MajorGroup("中国传媒大学");
        cumtUniversity.addMajor(new Major("新闻学"));
        cumtUniversity.addMajor(new Major("编导"));
        cumtUniversity.addMajor(new Major("通信工程"));
        cumtUniversity.addMajor(new Major("数字媒体"));
        majorGroups.put(16, cumtUniversity);

        // 志愿组17 - 北京中医药大学
        MajorGroup bcmpuUniversity = new MajorGroup("北京中医药大学");
        bcmpuUniversity.addMajor(new Major("中医学"));
        bcmpuUniversity.addMajor(new Major("中药学"));
        bcmpuUniversity.addMajor(new Major("针灸推拿"));
        bcmpuUniversity.addMajor(new Major("康复治疗"));
        majorGroups.put(17, bcmpuUniversity);

        // 志愿组18 - 首都师范大学
        MajorGroup cfuUniversity = new MajorGroup("首都师范大学");
        cfuUniversity.addMajor(new Major("汉语言文学（师范）"));
        cfuUniversity.addMajor(new Major("数学（师范）"));
        cfuUniversity.addMajor(new Major("音乐（师范）"));
        cfuUniversity.addMajor(new Major("英语（师范）"));
        majorGroups.put(18, cfuUniversity);

        // 志愿组19 - 北京林业大学
        MajorGroup bfuUniversity = new MajorGroup("北京林业大学");
        bfuUniversity.addMajor(new Major("林学"));
        bfuUniversity.addMajor(new Major("园艺"));
        bfuUniversity.addMajor(new Major("动物科学"));
        bfuUniversity.addMajor(new Major("环境工程"));
        majorGroups.put(19, bfuUniversity);

        // 志愿组20 - 北京体育大学
        MajorGroup bsuUniversity = new MajorGroup("北京体育大学");
        bsuUniversity.addMajor(new Major("体育教育"));
        bsuUniversity.addMajor(new Major("运动训练"));
        bsuUniversity.addMajor(new Major("体育管理"));
        majorGroups.put(20, bsuUniversity);

        // 志愿组21 - 北京电影学院
        MajorGroup bfaUniversity = new MajorGroup("北京电影学院");
        bfaUniversity.addMajor(new Major("导演"));
        bfaUniversity.addMajor(new Major("表演"));
        bfaUniversity.addMajor(new Major("摄影"));
        bfaUniversity.addMajor(new Major("编剧"));
        majorGroups.put(21, bfaUniversity);

        // 志愿组22 - 北京舞蹈学院
        MajorGroup bdaUniversity = new MajorGroup("北京舞蹈学院");
        bdaUniversity.addMajor(new Major("舞蹈表演"));
        bdaUniversity.addMajor(new Major("舞蹈编导"));
        majorGroups.put(22, bdaUniversity);
        MajorGroup bfsuUniversity = new MajorGroup("北京外国语大学");
        bfsuUniversity.addMajor(new Major("英语"));
        bfsuUniversity.addMajor(new Major("法语"));
        bfsuUniversity.addMajor(new Major("德语"));
        bfsuUniversity.addMajor(new Major("日语"));
        bfsuUniversity.addMajor(new Major("俄语"));
        majorGroups.put(23, bfsuUniversity);

        // 志愿组24 - 北京大学医学部
        MajorGroup pekingUniversityMedical = new MajorGroup("北京大学医学部");
        pekingUniversityMedical.addMajor(new Major("临床医学"));
        pekingUniversityMedical.addMajor(new Major("口腔医学"));
        pekingUniversityMedical.addMajor(new Major("公共卫生"));
        pekingUniversityMedical.addMajor(new Major("护理学"));
        majorGroups.put(24, pekingUniversityMedical);

        // 志愿组25 - 北京科技大学
        MajorGroup bstuUniversity = new MajorGroup("北京科技大学");
        bstuUniversity.addMajor(new Major("金属材料"));
        bstuUniversity.addMajor(new Major("冶金工程"));
        bstuUniversity.addMajor(new Major("自动化"));
        bstuUniversity.addMajor(new Major("计算机科学"));
        majorGroups.put(25, bstuUniversity);

        // 志愿组26 - 北京语言大学
        MajorGroup blcuUniversity = new MajorGroup("北京语言大学");
        blcuUniversity.addMajor(new Major("汉语言文学"));
        blcuUniversity.addMajor(new Major("外国语言文学"));
        blcuUniversity.addMajor(new Major("教育学"));
        blcuUniversity.addMajor(new Major("翻译"));
        majorGroups.put(26, blcuUniversity);

        // 志愿组27 - 北京经济技术开发区高校
        MajorGroup bjeduUniversity = new MajorGroup("北京经济技术开发区高校");
        bjeduUniversity.addMajor(new Major("国际贸易"));
        bjeduUniversity.addMajor(new Major("市场营销"));
        bjeduUniversity.addMajor(new Major("金融学"));
        bjeduUniversity.addMajor(new Major("会计学"));
        majorGroups.put(27, bjeduUniversity);

        // 志愿组28 - 北京城市学院
        MajorGroup bcuUniversity = new MajorGroup("北京城市学院");
        bcuUniversity.addMajor(new Major("城市管理"));
        bcuUniversity.addMajor(new Major("房地产"));
        bcuUniversity.addMajor(new Major("环境科学"));
        majorGroups.put(28, bcuUniversity);

        // 志愿组29 - 北京信息科技大学
        MajorGroup bituUniversity = new MajorGroup("北京信息科技大学");
        bituUniversity.addMajor(new Major("计算机科学"));
        bituUniversity.addMajor(new Major("信息管理"));
        bituUniversity.addMajor(new Major("电子商务"));
        bituUniversity.addMajor(new Major("通信工程"));
        majorGroups.put(29, bituUniversity);

        // 志愿组30 - 北京协和医学院
        MajorGroup pumsUniversity = new MajorGroup("北京协和医学院");
        pumsUniversity.addMajor(new Major("临床医学"));
        pumsUniversity.addMajor(new Major("药学"));
        pumsUniversity.addMajor(new Major("公共卫生"));
        majorGroups.put(30, pumsUniversity);

        // 志愿组31 - 北京工商大学
        MajorGroup bbuUniversity = new MajorGroup("北京工商大学");
        bbuUniversity.addMajor(new Major("工商管理"));
        bbuUniversity.addMajor(new Major("财务管理"));
        bbuUniversity.addMajor(new Major("国际贸易"));
        bbuUniversity.addMajor(new Major("市场营销"));
        majorGroups.put(31, bbuUniversity);

        // 志愿组32 - 北京石油化工学院
        MajorGroup bppuUniversity = new MajorGroup("北京石油化工学院");
        bppuUniversity.addMajor(new Major("石油工程"));
        bppuUniversity.addMajor(new Major("化学工程"));
        bppuUniversity.addMajor(new Major("能源与动力"));
        majorGroups.put(32, bppuUniversity);

        // 志愿组33 - 北京化工大学
        MajorGroup bcutUniversity = new MajorGroup("北京化工大学");
        bcutUniversity.addMajor(new Major("化学工程"));
        bcutUniversity.addMajor(new Major("环境工程"));
        bcutUniversity.addMajor(new Major("化学"));
        majorGroups.put(33, bcutUniversity);

        // 志愿组34 - 北京理工大学珠海学院
        MajorGroup bitzhUniversity = new MajorGroup("北京理工大学珠海学院");
        bitzhUniversity.addMajor(new Major("机械设计"));
        bitzhUniversity.addMajor(new Major("电气工程"));
        bitzhUniversity.addMajor(new Major("计算机科学"));
        majorGroups.put(34, bitzhUniversity);

        // 志愿组35 - 北京联合大学
        MajorGroup bjuUniversity = new MajorGroup("北京联合大学");
        bjuUniversity.addMajor(new Major("信息管理"));
        bjuUniversity.addMajor(new Major("电子商务"));
        bjuUniversity.addMajor(new Major("广告学"));
        majorGroups.put(35, bjuUniversity);
    }
    // 输入学生信息
    private Student inputStudentInfo() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入学生姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入学号: ");
        String studentId = scanner.nextLine();

        System.out.print("请输入准考证号: ");
        String examNumber = scanner.nextLine();

        System.out.print("请输入身份证号: ");
        String idCard = scanner.nextLine();

        System.out.print("请输入高考成绩: ");
        int score = scanner.nextInt();
        scanner.nextLine();  // Consume the remaining newline

        return new Student(name, studentId, examNumber, idCard, score);
    }

    // 显示已选择的志愿
    private void showVoluntaryChoices(Student student) {
        student.printVoluntaryChoices();
    }

    // 填报志愿
    private void fillVoluntaryChoices(Student student) {
        Scanner scanner = new Scanner(System.in);
        int choice;

        do {
            System.out.println("===== 填报志愿 =====");
            System.out.println("==================== 可选高校及专业 ====================");

            // 展示高校及其专业
            for (Map.Entry<Integer, MajorGroup> entry : majorGroups.entrySet()) {
                MajorGroup majorGroup = entry.getValue();
                System.out.println(entry.getKey() + ". " + majorGroup.getUniversityName());
                int i = 1;
                for (Major major : majorGroup.getMajors()) {
                    System.out.println("    " + i++ + ". " + major.getName());
                }
            }

            System.out.println("====================================================");
            System.out.print("请输入你想选择的高校编号 (输入0退出，最多填报30个志愿): ");
            choice = scanner.nextInt();
            if (choice != 0 && majorGroups.containsKey(choice)) {
                MajorGroup selectedGroup = majorGroups.get(choice);
                System.out.print("请选择一个专业编号: ");
                int majorChoice = scanner.nextInt();
                if (majorChoice > 0 && majorChoice <= selectedGroup.getMajors().size()) {
                    Major selectedMajor = selectedGroup.getMajors().get(majorChoice - 1);
                    student.addVoluntaryChoice(selectedGroup.getUniversityName(), selectedMajor);
                    System.out.println("志愿填报成功！");
                } else {
                    System.out.println("无效的专业编号！");
                }
            }
        } while (choice != 0 && student.getVoluntaryChoices().size() < 30);
    }

    // 搜索所有可选的志愿（包括未选择的）
    private void searchAllChoices(String keyword) {
        boolean found = false;
        System.out.println("===== 搜索结果 =====");
        for (Map.Entry<Integer, MajorGroup> entry : majorGroups.entrySet()) {
            MajorGroup majorGroup = entry.getValue();
            for (Major major : majorGroup.getMajors()) {
                if (majorGroup.getUniversityName().contains(keyword) || major.getName().contains(keyword)) {
                    System.out.println("学校: " + majorGroup.getUniversityName() + ", 专业: " + major.getName());
                    found = true;
                }
            }
        }

        if (!found) {
            System.out.println("未找到匹配的志愿！");
        }
    }

    // 修改志愿顺序
    private void modifyVoluntaryOrder(Student student) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("===== 修改志愿顺序 =====");
        student.printVoluntaryChoices();

        if (student.getVoluntaryChoices().isEmpty()) {
            System.out.println("您尚未填报任何志愿！");
            return;
        }

        System.out.print("请输入您想交换位置的第一个志愿位置（1-" + student.getVoluntaryChoices().size() + "）：");
        int firstChoice = scanner.nextInt() - 1;

        System.out.print("请输入您想交换位置的第二个志愿位置（1-" + student.getVoluntaryChoices().size() + "）：");
        int secondChoice = scanner.nextInt() - 1;

        if (firstChoice >= 0 && firstChoice < student.getVoluntaryChoices().size() &&
                secondChoice >= 0 && secondChoice < student.getVoluntaryChoices().size() && firstChoice != secondChoice) {
            VoluntaryChoice temp = student.getVoluntaryChoices().get(firstChoice);
            student.getVoluntaryChoices().set(firstChoice, student.getVoluntaryChoices().get(secondChoice));
            student.getVoluntaryChoices().set(secondChoice, temp);
            System.out.println("志愿顺序修改成功！");
        } else {
            System.out.println("无效的志愿位置，请重新选择！");
        }
    }
    // 显示主菜单
    private void showMainMenu(Student student) {
        Scanner scanner = new Scanner(System.in);
        int choice;

        do {
            System.out.println("==================== 高考志愿填报系统 ====================");
            System.out.println("1. 查看个人志愿信息");
            System.out.println("2. 填报志愿");
            System.out.println("3. 修改志愿顺序");
            System.out.println("4. 打印志愿填报表");
            System.out.println("5. 搜索所有可选志愿");
            System.out.println("0. 退出系统");
            System.out.print("请选择操作: ");
            choice = scanner.nextInt();
            scanner.nextLine();  // Consume the remaining newline

            switch (choice) {
                case 1:
                    showVoluntaryChoices(student);
                    break;
                case 2:
                    fillVoluntaryChoices(student);
                    break;
                case 3:
                    modifyVoluntaryOrder(student);
                    break;
                case 4:
                    student.printVoluntaryChoices();
                    break;
                case 5:
                    // 搜索所有可选的志愿
                    System.out.print("请输入搜索关键字（学校或专业名称）: ");
                    String keyword = scanner.nextLine();
                    searchAllChoices(keyword);
                    break;
                case 0:
                    System.out.println("感谢使用高考志愿填报系统，退出中...");
                    break;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        } while (choice != 0);
    }

    // 启动系统
    public void start() {
        Scanner scanner = new Scanner(System.in);

        // 初始化高校和专业数据
        initializeMajorGroups();

        // 输入学生信息
        Student student = inputStudentInfo();

        // 显示主菜单
        showMainMenu(student);
    }

    public static void main(String[] args) {
        // 启动志愿填报系统
        AdmissionSystem admissionSystem = new AdmissionSystem();
        admissionSystem.start();
    }
}